package com.gzhuxj201.controller;

import com.gzhuxj201.entity.Result;
import com.gzhuxj201.entity.SC;
import com.gzhuxj201.entity.Student;
import com.gzhuxj201.service.SCService;
import com.gzhuxj201.util.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("sc")
@Slf4j
public class ScController {
    @Autowired
    private SCService scService;

    /**
     *老师给课程导入学生名单
     * @param file
     * @param cno
     * @return
     * @throws Exception
     */
    @PostMapping("/importStuToCourse")
    public Result importStuToCourse(@RequestParam(name = "file") MultipartFile file, @RequestParam(name = "cno") String cno) throws Exception {
        log.info("课程号: "+cno);
        scService.importStuToCourse(file,cno);
        return Result.success("导入成功");
    }

    @DeleteMapping("{cno}")
    public Result deselectByCno(@PathVariable String cno){
        Student student = (Student) UserHolder.getUser();
        SC sc = new SC(student.getSno(), cno);
        return scService.deselectByCno(sc);
    }

    @PostMapping
    public Result add(String cno){
        Student student = (Student) UserHolder.getUser();
        SC sc = new SC(student.getSno(), cno);
        return scService.add(sc);
    }
}

